import regeneratorRuntime from '../../../../utils/runtime.js';
import img_list from '../../../../utils/util.js';
import { getData, setData, clearData, dataKey } from '../../../../utils/storage.js';
import shareFun from '../../../../utils/shareFun.js';
import {
  saveVisit,
  search,
} from '../../../../utils/imapi.js';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height:200,
    cur_choose_index:-1,
    area_list:[
      {
        up:'0-39',
        low:'0',
        high:'39',
        down:'33%的选择'
      },
      {
        up:'39-139',
        low:'39',
        high:'139',
        down:'6%的选择'
      },
      {
        up:'139-339',
        low:'139',
        high:'339',
        down:'9%的选择'
      },
    ],
    index_commission_rate:0,
    commission_rate_list:[
      {"id":1,"name":"全部","low":"",'high':'',"is_selected":1},
      {"id":2,"name":"0-30","low":"0",'high':'0.3',"is_selected":0},
      {"id":3,"name":"30-60","low":"0.3",'high':'0.6',"is_selected":0},
      {"id":4,"name":"60%以上","low":"0.6",'high':'1.0',"is_selected":0},
    ],
    index_zk_rate:0,
    zk_rate_list:[
      {"id":1,"name":"全部","low":"",'high':'',"is_selecteds":1},
      {"id":2,"name":"0-30","low":"0",'high':'0.3',"is_selecteds":0},
      {"id":3,"name":"30-60","low":"0.3",'high':'0.6',"is_selecteds":0},
      {"id":4,"name":"60%以上","low":"0.6",'high':'1.0',"is_selecteds":0},
    ],
    is_min:true,
    networkType: 1,
    chooseIndex: 1,
    show: 1,
    max: '',
    priceIndex: 0,
    min: '',
    is_in_approve: 0,
    quality_name: '',
    tabIndex:0,
    keyword: '',
    sliderLeft:55,
    sliderOffset:150,
    show_title:false,
    bgColor:'white',
    chooseList:[
      {"id":1,"name":"综合","sort_name":"","is_selected":1},
      {"id":2,"name":"销量","sort_name":"sales","is_selected":1},
      {"id":3,"name":"价格","sort_name":"price","is_selected":1},
      {"id":4,"name":"利率","sort_name":"tk_rate","is_selected":1},
    ],
    tabs:[
      {"id":1,"name":"全网","p":"","is_selected":1},
      {"id":2,"name":"拼多多","p":"PDD","is_selected":1},
      {"id":3,"name":"唯品会","p":"VIP","is_selected":1},
      {"id":4,"name":"京东","p":"JD","is_selected":1},
      {"id":5,"name":"淘宝","p":"TB","is_selected":1},
    ],
    shadow:1,
	model:'自购模式',
    low_price:'',
    high_price:'',
    productList:[],
    id: '',
    is_can_buy: 0,
    hasVideo: 0,
    sys_key: '',
    title: '',
    total_count: '',
    page: 1,
    show_choose:0,
    top:app.globalData.CustomBar,
    inputValue:'',
    keyword: '',
    img_list:"img_list.img_list",
    commission_rate_visible:true, //是否显示佣金比率
    zk_mode:0, //0-自购模式  1-推客模式
    show_list:false,
    is_live:'',
    tipVisible:true, //显示底部添加商品提示的显示与隐藏
    is_coupon:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {

	  
  console.log(options);
      this.setData({
        img_list:img_list.img_list,
        is_live:options.is_live
      })

      if(options.is_live){
        this.setData({
          is_live:options.is_live, //直播标记 将打开添加到直播间
        })
      }


    if (app.globalData.configs.taobao_search_enable=='0' || app.globalData.is_audit){
      this.setData({

        tabs:[
          {"id":1,"name":"全网","p":"","is_selected":1},
          {"id":2,"name":"拼多多","p":"PDD","is_selected":1},
          {"id":3,"name":"唯品会","p":"VIP","is_selected":1},
          {"id":4,"name":"京东","p":"JD","is_selected":1},
          //{"id":5,"name":"淘宝","p":"TB","is_selected":1},
        ]
      })
    }

    var that = this;
    shareFun.hidenShareMenu();
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      commission_rate_from:options.commission_rate_from || '',
      commission_rate_to:options.commission_rate_to || '',
      zk_rate_from:options.zk_rate_from || '',
      zk_rate_to:options.zk_rate_to || '',
      price_from:options.price_from || '',
      price_to:options.price_to || '',
      p:options.p || '',
      keyword:options.keyword || '',
      tabIndex: options.tabIndex || 0,
      zk_mode:0,
      is_coupon:options.is_coupon,
      tagsList:options.keyword?[{name:options.keyword}]:[],
    },()=>{
      const query = wx.createSelectorQuery();
      query.select('.contanier').boundingClientRect();
      query.exec((res) => {
        that.setData({
          height:res[0].height
        })
      });
    })
    console.log(options.commission_rate_from)
    if(app.is_auth()){ //登陆后 才保存搜索记录
      await saveVisit({keyword:options.keyword ,user_id:app.globalData.userInfo.uid})
    }
   
    var {data} = await search({q:this.data.keyword,p: this.data.p,commission_rate_from: this.data.commission_rate_from,commission_rate_to: this.data.commission_rate_to,price_from:this.data.price_from,price_to:this.data.price_to,page_size:10,zk_rate_from: this.data.zk_rate_from,zk_rate_to: this.data.zk_rate_to,zk_mode:this.data.zk_mode,is_coupon:this.das})
    console.log(data);
	this.canLoadMore = true;
    this.page = 1
    wx.hideLoading({})
    this.setData({
      show:1,
      productList: data
    })
    // this.timeOutCloseTip()
  },
  // 删除tag
  showInput(e) {
    var that = this;
    var inputValue = this.data.tagsList[0].name;
    console.log(inputValue)
    that.setData({
      keyword:inputValue,
      inputValue,
      tagsList: []
    })
  },
  SetShadow(e) {
    this.setData({
      shadow: e.detail.value?1:0
    })
	this.confirm();
  },
  SetShadow1(e) {
    this.setData({
      zk_mode: e.detail.value?1:0
    })
	if(e.detail.value){
		this.setData({
      model:'推手模式',
      zk_mode:1,
		})
	}else{
		this.setData({
      model:'自购模式',
      zk_mode:0,
		})
	}
	console.log(e.detail.value)
	console.log(this.data.zk_mode);
  },
  deleteItem(){
    this.setData({
      keyword:'',
      tagsList: []
    })
  },
  async onTabClick(event) {
    let index = event.detail.index;
    this.data.tabIndex = index
    this.page = 1;

    let q = this.data.keyword || ''

    if(app.globalData.is_audit){
   // 禁用关键词搜索判断
      if(app.is_disable_keywords(q)){

        wx.showToast({
          title: '暂不支持该关键词搜索',
        })
        return 

      }
       

    }


    var {data} = await search({
      q:this.data.keyword || '',
      p: this.data.tabs[index].p || '',
      sort_name:this.data.chooseList[this.data.chooseIndex - 1].sort_name,
      sort:this.data.chooseIndex==1?'':(this.data.is_min?'desc':'asc'),
      page_index:this.page,
      is_coupon:this.data.shadow
    })
    this.setData({
      productList: data
    })
  },
  showListOrBox(){
    this.setData({
      show_list:!this.data.show_list
    })
  },
  showChoose(){
    this.setData({
      show_choose:1
    })
  },
  hideChoose(){
    this.setData({
      show_choose:0,
    })
  },
  async tabClick(e) {
    var index = e.currentTarget.dataset.index
    if(index == this.data.chooseIndex && (index == 3 || index == 2)){
      this.setData({
        chooseIndex: index,
        is_min:!this.data.is_min
      })
    } else{
      this.setData({
        is_min:true,
        chooseIndex: index
      })
    }
    this.page = 1;
    var commission_rate_from = this.data.commission_rate_list[this.data.index_commission_rate].low;
    var commission_rate_to = this.data.commission_rate_list[this.data.index_commission_rate].high;
    var zk_rate_from = this.data.zk_rate_list[this.data.index_zk_rate].low;
    var zk_rate_to = this.data.zk_rate_list[this.data.index_zk_rate].high;
    var price_from = this.data.low_price;
    var price_to = this.data.high_price;
    var zk_mode=this.data.zk_mode
    var {data} = await search({
      q:this.data.keyword || '',
      p: this.data.tabs[this.data.tabIndex].p || '',
      sort_name:this.data.chooseList[this.data.chooseIndex - 1].sort_name,
      sort:this.data.chooseIndex==1?'':(this.data.is_min?'desc':'asc'),
      page_index:this.page,
      commission_rate_from,
      commission_rate_to,
      zk_rate_from,
      zk_rate_to,
      price_from,
      price_to,
      is_coupon:this.data.shadow,
      zk_mode:zk_mode
    })
    this.setData({
      productList: data
    })
  },
  getWord(e) {
    this.setData({
      keyword: e.detail.value,
      inputValue: e.detail.value,
    })
  },
  changeInput1(e){
    this.setData({
      low_price:e.detail.value
    })
  },
  changeInput2(e){
    this.setData({
      high_price:e.detail.value
    })
  },
  choosePrice(e){
    var index = e.currentTarget.dataset.index;
    var obj = this.data.area_list[index]
    if(index == this.data.cur_choose_index){
      this.setData({
        low_price:'',
        high_price:'',
        cur_choose_index:-1,
        is_choose_price:false
      })
    } else {
      this.setData({
        low_price:obj.low,
        high_price:obj.high,
        cur_choose_index:index,
        is_choose_price:true
      })
    }
  },
  chooseFromEarn(e){
    var index = e.currentTarget.dataset.index;
    this.data.commission_rate_list.forEach(element => {
      element.is_selected = 0
    });
    this.data.commission_rate_list[index].is_selected = 1
    this.setData({
      index_commission_rate:index,
      commission_rate_list: this.data.commission_rate_list
    })
  },
  chooseFromEarns(e){
    var index = e.currentTarget.dataset.index;
    this.data.zk_rate_list.forEach(element => {
      element.is_selecteds = 0
    });
    this.data.zk_rate_list[index].is_selecteds = 1
    this.setData({
      index_zk_rate:index,
      zk_rate_list: this.data.zk_rate_list
    })
  },
  reset(){
    this.data.commission_rate_list.forEach(element => {
      element.is_selected = 0
    });
    this.data.commission_rate_list[0].is_selected = 1
    this.data.zk_rate_list.forEach(element => {
      element.is_selecteds = 0
    });
    this.data.zk_rate_list[0].is_selecteds = 1
    this.setData({
      low_price:'',
      high_price:'',
      cur_choose_index:-1,
      is_choose_price:false,
      index_commission_rate:0,
      index_zk_rate:0,
      commission_rate_list: this.data.commission_rate_list,
      zk_rate_list: this.data.zk_rate_list,
	  zk_mode:1,
	  model:'推客模式'
    })
  },
  async confirm(){
    var commission_rate_from = this.data.commission_rate_list[this.data.index_commission_rate].low;
    var commission_rate_to = this.data.commission_rate_list[this.data.index_commission_rate].high;
    var zk_rate_from = this.data.zk_rate_list[this.data.index_zk_rate].low;
    var zk_rate_to = this.data.zk_rate_list[this.data.index_zk_rate].high;
    var price_from = this.data.low_price;
    var price_to = this.data.high_price;
    var q= this.data.keyword||'';
    
    

    this.page = 1;
    var {data} = await search({
      q:q,
      p: this.data.tabs[this.data.tabIndex].p || '',
      sort_name:this.data.chooseList[this.data.chooseIndex - 1].sort_name,
      sort:this.data.chooseIndex==1?'':(this.data.is_min?'desc':'asc'),
      page_index:this.page,
      commission_rate_from,
      commission_rate_to,
      zk_rate_from,
      zk_rate_to,
      price_from,
      price_to,
      is_coupon:this.data.shadow,
	  zk_mode:this.data.zk_mode
    })
	console.log(data);
    this.setData({
      show_choose:false,
      productList: data
    })
  },
  async startSearch() {
    var that = this;
    // if(!this.data.keyword){
    //   wx.showToast({
    //     title: '请输入搜索关键词',
    //     icon:"none"
    //   },2000)
    //   return;
    // }
    var recordList = await getData(dataKey.recordList);
    if (recordList.indexOf(this.data.keyword) == '-1' && this.data.keyword) {
      recordList.unshift(this.data.keyword);
      recordList = recordList.slice(0, 19);
      await setData(dataKey.recordList,recordList);
    }
    if(this.data.keyword){
      this.setData({
        tagsList:[{name:this.data.keyword}]
      })
    }
    var commission_rate_from = this.data.commission_rate_list[this.data.index_commission_rate].low;
    var commission_rate_to = this.data.commission_rate_list[this.data.index_commission_rate].high;
    var zk_rate_from = this.data.zk_rate_list[this.data.index_zk_rate].low;
    var zk_rate_to = this.data.zk_rate_list[this.data.index_zk_rate].high;
    var price_from = this.data.low_price;
    var price_to = this.data.high_price;
    

    var q =this.data.keyword || ''

    if(app.globalData.is_audit){

      if(q=='会员' || q=='充值' || q=='vip' || q=='VIP'){
        wx.showToast({
          title: '暂不支持虚拟商品类搜索',
        })
        return
      }
       
    }

    this.page = 1;
    var {data} = await search({
      q:q,
      p: this.data.tabs[this.data.tabIndex].p || '',
      sort_name:this.data.chooseList[this.data.chooseIndex - 1].sort_name,
      sort:this.data.chooseIndex==1?'':(this.data.is_min?'desc':'asc'),
      page_index:this.page,
      commission_rate_from,
      commission_rate_to,
      zk_rate_from,
      zk_rate_to,
      price_from,
      price_to,
      zk_mode:this.data.zk_mode,
    })
    this.setData({
      productList: data
    })
  },
  hasVideo() {
    this.setData({
      hasVideo: !this.data.hasVideo,
      chooseIndex: 0
    })
    this.refreshProductList();
  },
  show_zkmode_info(){

   

    wx.showModal({
		  	title: '提示',
		  	content: '推手模式：\r\n折扣率 = 优惠券价格 / 折扣价  自购模式: 折扣率=（优惠券+佣金）/ 折扣价',
		  	showCancel: false
		  	});
  },
  // 点击关闭按钮 关闭底部tip
  closeTip(e) {
    this.setData({
      tipVisible:false
    })
  },
  timeOutCloseTip() {
    var that = this
    setTimeout(function() {
      that.closeTip()
    },7000)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!app.globalData.userInfo || app.globalData.userInfo.level_code=='0'){
      this.setData({
        commission_rate_visible:false,
      })
   }
  },
  onReady:function(){
    
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: async function () {
    this.page = 1;
    var commission_rate_from = this.data.commission_rate_list[this.data.index_commission_rate].low;
    var commission_rate_to = this.data.commission_rate_list[this.data.index_commission_rate].high;
    var zk_rate_from = this.data.zk_rate_list[this.data.index_zk_rate].low;
    var zk_rate_to = this.data.zk_rate_list[this.data.index_zk_rate].high;
    var price_from = this.data.low_price;
    var price_to = this.data.high_price;
    var {data} = await search({
      q:this.data.keyword || '',
      p: this.data.tabs[this.data.tabIndex].p || '',
      sort_name:this.data.chooseList[this.data.chooseIndex - 1].sort_name,
      sort:this.data.chooseIndex==1?'':(this.data.is_min?'desc':'asc'),
      page_index:this.page,
      commission_rate_from,
      commission_rate_to,
      zk_rate_from,
      zk_rate_to,
      price_from,
      price_to,
      zk_mode:this.data.zk_mode,
    })
    this.setData({
      productList: data
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    if (!this.canLoadMore) return;
    wx.showLoading({
        title: this.page == 1 ? '加载中...' : '加载更多...',
    });
    var commission_rate_from = this.data.commission_rate_list[this.data.index_commission_rate].low || this.data.commission_rate_from;
    var commission_rate_to = this.data.commission_rate_list[this.data.index_commission_rate].high || this.data.commission_rate_to;
    var zk_rate_from = this.data.zk_rate_list[this.data.index_zk_rate].low || this.data.zk_rate_from;
    var zk_rate_to = this.data.zk_rate_list[this.data.index_zk_rate].high || this.data.zk_rate_to;
    var price_from = this.data.low_price || this.data.price_from;
    var price_to = this.data.high_price || this.data.price_to;
    var {data} = await search({
      q:this.data.keyword || '',
      p: this.data.tabs[this.data.tabIndex].p || '',
      sort_name:this.data.chooseList[this.data.chooseIndex - 1].sort_name,
      sort:this.data.chooseIndex==1?'':(this.data.is_min?'desc':'asc'),
      page_index:this.page + 1,
      commission_rate_from,
      commission_rate_to,
      price_from,
      price_to,
      zk_rate_from,
      zk_rate_to,
      zk_mode:this.data.zk_mode,
	  is_coupon:this.data.shadow
    })
    const arr = data;
    if (arr.length > 0) {
        this.page++;
        this.setData({
          productList: [...this.data.productList, ...arr],
        });
    } else {
        this.canLoadMore = false;
    }
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})